
package ling.archives.controller;

import com.easy.query.core.api.pagination.EasyPageResult;
import com.ruoyi.common.utils.DictUtils;
import io.swagger.annotations.*;
import ling.archives.service.ApprovalService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import ling.archives.domain.ApprovalRecord;
//import ling.archives.service.ApprovalRecordService;
import ling.archives.common.*;
import ling.archives.common.domain.*;

/** 审批记录 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-3-20
 */
@Api(tags = "审批记录 功能接口")
@RestController
@RequestMapping("/archives/ApprovalRecord")
public class ApprovalRecordController extends EQueryController<ApprovalRecord>{

    @Resource
    ApprovalService approvalService;
    @ApiOperation("详情")
    @PreAuthorize("@ar.any(@ar.approvalRecord.query)")
    @GetMapping(value = "/{id}")
    public Result<ApprovalRecord> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }
    @ApiOperation("分页列表")
    @PreAuthorize("@ar.any(@ar.approvalRecord.list)")
    @GetMapping("list")
    public ResultRows<ApprovalRecord> list(ApprovalRecord dto, PageDomain pageRequest) {

        //审批模板来自字段，如果当前人员审批权限==字典值，审批人
        //新建审批流程
        //审批：
        // 查询审批记录：待审批、已审批


        return queryPage(dto,pageRequest);
    }

    @ApiOperation("新增")
    @PreAuthorize("@ar.any(@ar.approvalRecord.add)")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @Transactional
    public Result add(@Validated @RequestBody ApprovalRecord dto) {
        var tmplType="ar_aa_approval";
        var formData="数据id";
        var title="";
        //创建根记录
        var re =approvalService.createWF(tmplType,dto.getFlowTitle(),dto.getFormData(),dto.getRemark());
//        entity.setStatus(ApprovalRecord.StatusWait);
//        var node=new ApprovalRecord();
//        node.setFlowId(entity.getFlowId());
//        node.setFlowType(entity.getFlowType());
//        node.setBody("1");
//        node.setType("1");
//        var roleArr=DataUtil.toObjByJson(entity.getBody(),String[].class);
//        node.setNodeRole(roleArr[0]);
//        node.setStatus(ApprovalRecord.StatusWait);
//        node.setCreatedTime(new Date());
//        node.setCreatedId(getUserId());
        return re;
    }


    /** 查询当前用户审批列表
     *
     * @param dto
     * @param pageSort
     * @return
     */
    @ApiOperation("查询当前用户审批列表")
    @PreAuthorize("@ar.any(@ar.approvalRecord.list)")
    @GetMapping("approvalList")
    public ResultRows<ApprovalRecord> approvalList(ApprovalRecord dto,PageSort pageSort) {
        var page = approvalService.approvalList(dto, pageSort);
        return page;
    }




    /** 审批,返回流程状态
     *
     * @param nodeId
     * @param isPass
     * @param remark
     */
    @ApiOperation("审批")
    @PreAuthorize("@ar.any(@ar.approvalRecord.add)")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping("Approval")
    @Transactional
    public Result<String> Approval(Long nodeId,boolean isPass,String remark) {
        var re=approvalService.Approval(nodeId,isPass,remark);
       return Result.restResult(re.getData().getStatus(), re.getCode(),re.getMsg());
    }
    @ApiOperation("删除")
    @PreAuthorize("@ar.any(@ar.approvalRecord.remove)")
    @Log(title = "审批记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation("编辑")
    @PreAuthorize("@ar.any(@ar.approvalRecord.edit)")
    @Log(title = "审批记录", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  Result edit(@Validated @RequestBody ApprovalRecord entity){
        return super.edit(entity);
    }
    @ApiOperation("导出")
    @PreAuthorize("@ar.any(@ar.approvalRecord.export)")
    @Log(title = "审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApprovalRecord entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }
}